package com.eastfair.venueservice.service.impl;

import com.eastfair.core.exception.BizException;
import com.eastfair.home.dto.ReportSpaceAuditDTO;
import com.eastfair.pay.dto.TransactionOrderDTO;
import com.eastfair.venueservice.dto.BondManageDTO;
import com.eastfair.venueservice.dto.ServiceOrderDTO;
import com.eastfair.venueservice.entity.BondManage;
import com.eastfair.venueservice.entity.ServiceOrder;
import com.eastfair.venueservice.exceptioncode.VenueServiceExceptionCode;
import com.eastfair.venueservice.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 搭建商报馆必缴订单服务
 *
 * @author dqq
 * @date 2023 /6/9
 */
@Service
@Slf4j
public class ReportSpaceMustPayOrderServiceImpl implements ReportSpaceMustPayOrderService {

    @Resource
    private HomeServiceOrderService homeServiceOrderService;

    @Resource
    private HomeBondManageService homeBondManageService;

    @Resource
    private VenueManageTransactionOrderService venueManageTransactionOrderService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveMustPayOrder(ReportSpaceAuditDTO reportSpaceAuditDTO) {
        log.info("saveMustPayOrder - 主场报馆审核通过生成服务订单，管理费订单，或保证金订单, reportSpaceAuditDTO={}", reportSpaceAuditDTO);
        // 生成服务订单或管理费订单
        List<ServiceOrder> serviceOrderList = homeServiceOrderService.saveOrderForReportSpace(reportSpaceAuditDTO);
        // 生成保证金订单
        BondManage bondManage = homeBondManageService.submitForReportSpace(reportSpaceAuditDTO);
        // 统一生成支付系统交易订单
        saveTransactionOrder(serviceOrderList, bondManage);
        return true;
    }

    @Override
    public boolean saveTransactionOrder(List<ServiceOrder> serviceOrderList, BondManage bondManage) {
        // 支付系统订单列表
        List<TransactionOrderDTO> transactionOrderList = new ArrayList<>();
        // 待发送站内信订单列表
        List<ServiceOrderDTO> serviceOrderListForMsg = new ArrayList<>();
        // 服务订单，管理费订单生成支付系统订单
        if (serviceOrderList != null && !serviceOrderList.isEmpty()) {
            for (ServiceOrder serviceOrder : serviceOrderList) {
                ServiceOrderDTO serviceOrderDTO = homeServiceOrderService.completeForTransactionOrder(serviceOrder);
                TransactionOrderDTO transactionOrderDTO = ServiceOrderService.toTransactionOrder(serviceOrderDTO);
                serviceOrderListForMsg.add(serviceOrderDTO);
                transactionOrderList.add(transactionOrderDTO);
            }
        }
        // 保证金订单生成支付系统订单
        BondManageDTO bondManageDTO = null;
        if (bondManage != null) {
            bondManageDTO = homeBondManageService.completeForTransactionOrder(bondManage);
            TransactionOrderDTO transactionOrderDTO = BondManageService.toTransactionOrder(bondManageDTO);
            transactionOrderList.add(transactionOrderDTO);
        }
        if (transactionOrderList.isEmpty()) {
            return false;
        }
        try {
            venueManageTransactionOrderService.batchSave(transactionOrderList);
        } catch (BizException e) {
            // 发送配置收款账号的站内信
            if (e.getCode() != VenueServiceExceptionCode.PAYEE_NOT_CONFIG.getCode()) {
                throw e;
            }
            if (serviceOrderList != null && !serviceOrderList.isEmpty()) {
                for (ServiceOrderDTO serviceOrderDTO : serviceOrderListForMsg) {
                    homeServiceOrderService.asyncSendConfigPayeeInnerMsg(serviceOrderDTO);
                }
            }
            if (bondManageDTO != null) {
                homeBondManageService.asyncSendConfigPayeeInnerMsg(bondManageDTO);
            }
            return true;
        }
        return true;
    }
}